﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;
using System.Web;

namespace Ruandao.HostServer.Sql
{
    using Ruandao.HostServer.SqlEntityAccess;
    using System.Configuration;

    public class JsonHostServerContext : HostServerContext
    {
        public IDictionary<int, Database> GetAllDatabases()
        {
            ObjectCache cache = MemoryCache.Default;
            string cacheKey = "ALL_DATABASE_OBJECTS";
            IDictionary<int, Database> databases = cache[cacheKey] as IDictionary<int, Database>;
            if (databases == null)
            {
                databases = new Dictionary<int, Database>();
                foreach (var e in DatabaseEntity.GetAllEntities())
                {
                    databases.Add(e.DatabaseId, new SqlDatabase().SetFromEntity(e));
                }
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(cacheKey, databases, policy);
            }
            return databases;
        }

        public IDictionary<string, Domain> GetAllDomains()
        {
            ObjectCache cache = MemoryCache.Default;
            string cacheKey = "ALL_DOMAIN_OBJECTS";
            IDictionary<string, Domain> domains = cache[cacheKey] as IDictionary<string, Domain>;
            if (domains == null)
            {
                domains = new Dictionary<string, Domain>();
                foreach (var e in DomainEntity.GetAllEntities())
                {
                    domains.Add(e.DomainName.ToLower(), new SqlDomain().SetFromEntity(e));
                }
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(cacheKey, domains, policy);
            }
            return domains;
        }

        public IDictionary<int, FileStorage> GetAllFileStorages()
        {
            ObjectCache cache = MemoryCache.Default;
            string cacheKey = "ALL_FILESTORAGE_OBJECTS";
            IDictionary<int, FileStorage> fileStorages = cache[cacheKey] as IDictionary<int, FileStorage>;
            if (fileStorages == null)
            {
                fileStorages = new Dictionary<int, FileStorage>();
                foreach (var e in FileStorageEntity.GetAllEntities())
                {
                    fileStorages.Add(e.FileStorageId, new SqlFileStorage().SetFromEntity(e));
                }
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(cacheKey, fileStorages, policy);
            }
            return fileStorages;
        }


        public Domain GetCurrentDomain()
        {
            string dnsSafeHost = HttpContext.Current.Request.Url.DnsSafeHost.ToLower();
            var domains = this.GetAllDomains();
            if (domains.ContainsKey(dnsSafeHost))
            {
                return domains[dnsSafeHost];
            }
            return null;
        }

        public Site GetSite(string domain)
        {
            Guid siteGuid = this.GetAllDomains()[domain].SiteGuid;
            return GetSite(siteGuid);
        }


        public Site GetSite(Guid siteGuid)
        {
            ObjectCache cache = MemoryCache.Default;
            string siteCacheKey = "SITEOBJECT_" + siteGuid;

            Site siteObject = cache[siteCacheKey] as Site;
            if (siteObject == null)
            {
                siteObject = new SqlSite();
                siteObject.SetFromEntity(SiteEntity.Fetch(siteGuid));

                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(siteCacheKey, siteObject, policy);
            }
            return siteObject;
        }

        public Site GetCurrentSite()
        {
            string dnsSafeHost = HttpContext.Current.Request.Url.DnsSafeHost.ToLower();
            Site siteObject = GetSite(dnsSafeHost);
            return siteObject;
        }
        // 获取当前的网件实例

        public static string SiteWareName
        {
            get
            {
                string name = ConfigurationManager.AppSettings["SitewareName"];
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new Exception("必须在appSettings中使用SitewareName名称来设置网件的名称。");
                }
                return name;
            }

        }

        public static SiteWare GetCurrent()
        {
            ObjectCache cache = MemoryCache.Default;
            string cachekey = "SITEWAREOBJECT_" + HostServerContext.GetInstance().GetCurrent().SiteGuid + "_" + SiteWareName;
            SiteWare ware = cache[cachekey] as SiteWare;

            if (ware == null)
            {
                ware = new SiteWare();
                ware.SetFromEntity(SiteWareEntity.FetchByUIX_SiteGuid_WareName(Site.GetCurrent().SiteGuid, SiteWareName));
                ware.WareName = SiteWareName;
                CacheItemPolicy policy = new CacheItemPolicy();

                cache.Set(cachekey, ware, policy);
            }
            return ware;
        }

    }
}
